package com.wanshi.controller;

import com.wanshi.po.User;
import com.wanshi.service.UserService;
import com.wanshi.utils.CreateVerificationCode;
import com.wanshi.utils.CreateVerificationCodeImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@SuppressWarnings("all")
@Controller
public class LoginController {

    @Autowired
    private UserService userService;


    @RequestMapping("/jump")
    public String jump(HttpServletRequest request, HttpServletResponse response,HttpSession session, String usertoken, Model model) {


        String[] items = usertoken.split("#");
        String username = items[0];
        String password = items[1];
        String shijian = items[2];
        long mills = System.currentTimeMillis();
        System.out.println(mills);
        long userShijian = Long.parseLong(shijian);
        long jiange = mills - userShijian;


        if(jiange > 0 && jiange < 1000L*60*5 ){
            //时间校验通过
            User user = userService.findUserByNameAndPsd(username,password);
            //3 提示
            if(user == null){
                model.addAttribute("error","用户名或密码错误");
                return "login";
            }else{
                //存在
                //保存登录状态
                session.setAttribute("user",user);
                Cookie cookie = new Cookie("JSESSIONID",session.getId());
                cookie.setMaxAge(1800);
                cookie.setPath(request.getContextPath());
                response.addCookie(cookie);

                return "redirect:index";
            }
        }else{
            model.addAttribute("error","用户名或密码错误");
            return "login";
        }
    }

    @RequestMapping("/imageCode")
    public void imageCode(HttpServletResponse resp,HttpSession session) throws IOException {
        String vericode= CreateVerificationCode.getSecurityCode();
        System.out.println(vericode);
        session.setAttribute("verityCode",vericode);
        //设置返回的内容
        resp.setContentType("img/jpeg");
        //浏览器不缓存响应内容--验证码图片，点一次就要刷新一次，所以不能有缓存出现
        resp.setHeader("Pragma","No-cache");
        resp.setHeader("Cache-Control","no-cache");
        //设置验证码失效时间
        resp.setDateHeader("Expires",0);
        //以字节流发过去，交给img的src属性去解析即可
        ImageIO.write(new CreateVerificationCodeImage(vericode).createImage(),"JPEG",resp.getOutputStream());
    }

    @RequestMapping("/login")
    public String login(HttpServletRequest request, HttpServletResponse response,HttpSession session, String username, String password, Model model) {
        /*String verityCode = (String)session.getAttribute("verityCode");
        if(  !code.equals(verityCode)){
            model.addAttribute("error","验证码错误");
            return "login";
        }*/
        User user = userService.findUserByNameAndPsd(username,password);

        //3 提示
        if(user == null){
            model.addAttribute("error","用户名或密码错误");
            return "login";
        }else{
            //存在
            //保存登录状态
            session.setAttribute("user",user);
            Cookie cookie = new Cookie("JSESSIONID",session.getId());
            cookie.setMaxAge(1800);
            cookie.setPath(request.getContextPath());
            response.addCookie(cookie);

            return "redirect:index";
        }
    }

    @RequestMapping("/index")
    public String index() {
        return "WEB-INF/pages/index";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        if(session != null){
            session.invalidate();
        }
        return "redirect:/login.jsp";
    }

    @RequestMapping("/dati/modifyPsd")
    public String modifyPsd(HttpSession session,String oldpsd,String newpsd,Model model) {
        User user = (User)session.getAttribute("user");
        if(!oldpsd.equals(user.getPassword())){
            model.addAttribute("error","旧密码错误");
            return "modifyPsd";
        }else{
            userService.modifyPsd(user.getId(),newpsd);
            user.setPassword(newpsd);
            session.setAttribute("user",user);
            model.addAttribute("error","恭喜您,密码修改成功!");
            return "modifyPsd";
        }
    }

}
